package com.cac.demo.newCode.compreQuery.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cac.demo.newCode.compreQuery.dto.*;
import com.cac.demo.newCode.compreQuery.mapper.ComPackAoQueryMapper;
import com.cac.demo.newCode.compreQuery.service.ComPackAoQueryService;
import com.cac.demo.newCode.compreQuery.vo.*;
import com.cac.demo.newCode.utils.Constants;
import com.cac.demo.util.adapter.ResponseMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author zty
 * @description
 * @date 2024/10/22
 */
@Service
public class ComPackAoQueryServiceImpl implements ComPackAoQueryService {

    //记录日志
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ComPackAoQueryMapper comPackAoQueryMapper;


    /**
     * 查询AO列表
     *
     * @param queryAoRequestDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> queryAOList(QueryAoRequestDTO queryAoRequestDTO) {
        //参数校验
        if (queryAoRequestDTO == null || queryAoRequestDTO.getSize() <= 0 || queryAoRequestDTO.getCurrent() <= 0) {
            logger.error("Invalid input parameters: queryAoRequestDTO={}, pageNum={}, pageSize={}", queryAoRequestDTO);
            return new ResponseMsg<>().setRetCode("400").setMessage("请检查输入参数!");
        }
        try {
            //动态设置分页参数
            Page<?> page = new Page<>(queryAoRequestDTO.getCurrent(), queryAoRequestDTO.getSize());
            IPage<QueryAoListRespVO> queryAoListRespVOIPage = comPackAoQueryMapper.queryAOList(page, queryAoRequestDTO);
            return new ResponseMsg<>().setResponseBody(queryAoListRespVOIPage).setRetCode("200").setMessage("响应成功!");
        } catch (Exception e) {
            // 异常处理
            logger.error("查询 AO 列表时出错", e);
            return new ResponseMsg<>().setRetCode("500").setMessage("内部服务器错误！");
        }

    }


    /**
     * 查询配套清单列表
     *
     * @param queryAoBillInfoDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> queryAoBillInfoList(QueryAoBillInfoDTO queryAoBillInfoDTO) {
        try {
            List<QueryAoBillInfoVO> queryAoBillInfoVOList = comPackAoQueryMapper.queryAoBillInfoList(queryAoBillInfoDTO);
            return new ResponseMsg<>().setResponseBody(queryAoBillInfoVOList).setRetCode(Constants.SUCCESS_CODE).setMessage(Constants.SUCCESS_MESSAGE);

        } catch (Exception e) {
            // 异常处理
            logger.error("查询配套清单列表时出错", e);
            return new ResponseMsg<>().setRetCode(Constants.ERROR_CODE).setMessage(Constants.ERROR_MESSAGE);
        }
    }

    /**
     * 查询物料清单列表
     *
     * @param queryMatchInfoDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> queryMatchInfoList(QueryMatchInfoDTO queryMatchInfoDTO) {
        try {
            List<QueryMatchInfoVO> queryMatchInfoList = comPackAoQueryMapper.queryMatchInfoList(queryMatchInfoDTO);
            return new ResponseMsg<>().setResponseBody(queryMatchInfoList).setRetCode(Constants.SUCCESS_CODE).setMessage(Constants.SUCCESS_MESSAGE);
        } catch (Exception e) {
            // 异常处理
            logger.error("查询物料清单列表时出错", e);
            return new ResponseMsg<>().setRetCode(Constants.ERROR_CODE).setMessage(Constants.ERROR_MESSAGE);
        }
    }

    /**
     * 查询工作包列表（分页）
     *
     * @param queryPackageListDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> searchPackageList(QueryPackageListDTO queryPackageListDTO) {
        try {
            //动态设置分页参数
            Page<?> page = new Page<>(queryPackageListDTO.getCurrent(), queryPackageListDTO.getSize());
            IPage<QueryPackageListVO> queryPackageListVOIPage = comPackAoQueryMapper.searchPackageList(page, queryPackageListDTO);
            return new ResponseMsg<>().setResponseBody(queryPackageListVOIPage).setRetCode(Constants.SUCCESS_CODE).setMessage(Constants.SUCCESS_MESSAGE);
        } catch (Exception e) {
            // 异常处理
            logger.error("查询工作包列表时出错", e);
            return new ResponseMsg<>().setRetCode(Constants.ERROR_CODE).setMessage(Constants.ERROR_MESSAGE);
        }
    }

    /**
     * 根据工作包ID查询AO列表信息
     *
     * @param queryPackageAOListDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> queryPackageAOList(QueryPackageAOListDTO queryPackageAOListDTO) {
        try {
            List<QueryPackageListVO> queryPackageList = comPackAoQueryMapper.queryPackageAOList(queryPackageAOListDTO);
            return new ResponseMsg<>().setResponseBody(queryPackageList).setRetCode(Constants.SUCCESS_CODE).setMessage(Constants.SUCCESS_MESSAGE);
        } catch (Exception e) {
            // 异常处理
            logger.error("查询AO列表时出错", e);
            return new ResponseMsg<>().setRetCode(Constants.ERROR_CODE).setMessage(Constants.ERROR_MESSAGE);
        }
    }

    /**
     * 根据AOid查询物料清单(工作包-》AO列表)
     *
     * @param queryPackageAoMatchDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> queryPackageAoMatch(QueryPackageAoMatchDTO queryPackageAoMatchDTO) {

        try {
            List<QueryPackageAoMatchVO> queryPackageAoMatchList = comPackAoQueryMapper.queryPackageAoMatch(queryPackageAoMatchDTO);
            return new ResponseMsg<>().setResponseBody(queryPackageAoMatchList).setRetCode(Constants.SUCCESS_CODE).setMessage(Constants.SUCCESS_MESSAGE);
        } catch (Exception e) {
            // 异常处理
            logger.error("查询AO列表下的物料清单时出错", e);
            return new ResponseMsg<>().setRetCode(Constants.ERROR_CODE).setMessage(Constants.ERROR_MESSAGE);
        }
    }

    /**
     * 查询匹配物料清单(工作包-》AO列表)
     *
     * @param queryPackageAoFaciliDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> queryPackageAoFacili(QueryPackageAoFaciDTO queryPackageAoFaciliDTO) {
        try {
            List<QueryPackageAoFaciVO> queryPackageAoFaciList = comPackAoQueryMapper.queryPackageAoFacili(queryPackageAoFaciliDTO);
            return new ResponseMsg<>().setResponseBody(queryPackageAoFaciList).setRetCode(Constants.SUCCESS_CODE).setMessage(Constants.SUCCESS_MESSAGE);
        } catch (Exception e) {
            // 异常处理
            logger.error("查询AO列表下的物料清单时出错", e);
            return new ResponseMsg<>().setRetCode(Constants.ERROR_CODE).setMessage(Constants.ERROR_MESSAGE);
        }
    }

    /**
     * 查询分拣计划管理总数及列表信息
     *
     * @param programManageDTO
     * @return
     */
    @Override
    public ResponseMsg<Object> queryProgramManagCount(ProgramManageDTO programManageDTO) {
        try {
            List<ProgramManagCountVO> programManagCountVOS = comPackAoQueryMapper.queryProgramManagCount();
            // 创建哈希表存储sortingIp和对应的ProgramManagerVO 列表
            Map<String, List<ProgramManagerVO>> twoWeeksListMap = new HashMap<>();
            Map<String, List<ProgramManagerVO>> monthListMap = new HashMap<>();
            // 查询14天未分拣完的数据
            List<ProgramManagerVO> programManagCountVOS1 = comPackAoQueryMapper.queryProgramManag14DaysList(programManageDTO);
            // 查询30天分拣完成的数据
            List<ProgramManagerVO> programManagCountVOS2 = comPackAoQueryMapper.queryProgramManag30DaysList(programManageDTO);
            // 填充 twoWeeksListMap
            for (ProgramManagerVO vo : programManagCountVOS1) {
                String sortingIp = vo.getSortingIp();
                twoWeeksListMap.computeIfAbsent(sortingIp, k -> new ArrayList<>()).add(vo);
            }
            // 填充monthListMap
            for (ProgramManagerVO vo : programManagCountVOS2) {
                String sortingIp = vo.getSortingIp();
                monthListMap.computeIfAbsent(sortingIp, k -> new ArrayList<>()).add(vo);
            }
            // 遍历programManagCountVOS 根据sortingIp设置daysList和monthlist
            for (ProgramManagCountVO program : programManagCountVOS) {
                String sortingIp = program.getSortingIp();
                program.setTwoWeeksList(twoWeeksListMap.getOrDefault(sortingIp, Collections.emptyList()));
                program.setMonthList(monthListMap.getOrDefault(sortingIp, Collections.emptyList()));
            }
            HashMap<String, List<ProgramManagCountVO>> hashMap = new HashMap<>();
            hashMap.put("countList", programManagCountVOS);
            return new ResponseMsg<>().setResponseBody(hashMap).setRetCode(Constants.SUCCESS_CODE).setMessage(Constants.SUCCESS_MESSAGE);
        } catch (Exception e) {
            // 异常处理
            logger.error("查询计划管理统计分拣数据出错", e);
            return new ResponseMsg<>().setRetCode(Constants.ERROR_CODE).setMessage(Constants.ERROR_MESSAGE);
        }
    }
}
